# -*- coding:utf-8 -*-
from constant import constant_define
from dao.MySqlDAL import MySqlDAL
import requests
import utils.CookieUtils as cu
import time
import datetime
__author__ = 'shudongma.msd(风骐)'


headers = {
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
        'cache-control':'no-cache',
        'Connection':'keep-alive',
        'User-Agent': constant_define.user_agent,
        'Referer':'http://xueqiu.com/hq/screener/CN',
        'Host':'xueqiu.com',
        'X-Requested-With':'XMLHttpRequest',
        'Cookie': cu.init_cookie()
}


sqlDao = MySqlDAL()

# 加载历史大盘数据
def travelSSE():
    req = requests.get('http://xueqiu.com/stock/forchartk/stocklist.json?symbol=SH000001&period=1day&type=normal&begin=1443545096354&end=1457183410230',headers=headers).json()
    charlist = req['chartlist']

    source_data = []
    for row in charlist:
        source_data.append((row['close'],row['percent'],datetime.datetime.strptime(row['time'],'%a %b %d %H:%M:%S +0800 %Y').date()))
        # print row['close'],row['percent'],datetime.datetime.strptime('Thu Oct 08 00:00:00 +0800 2015','%a %b %d %H:%M:%S +0800 %Y').date()

    print sqlDao.insert_many_batch(['price','percent','date'],source_data,'tb_sse_info',50)


# 加载今日大盘数据
def loadSSE():
    req = requests.get('http://xueqiu.com/v4/stock/quote.json?code=SH000001',headers=headers).json()['SH000001']
    if str(datetime.date.today()) == str(datetime.datetime.strptime(req['time'],'%a %b %d %H:%M:%S +0800 %Y').date()):
        print sqlDao.insert_data({'price':req['close'],'percent':req['percentage'],'date':datetime.datetime.strptime(req['time'],'%a %b %d %H:%M:%S +0800 %Y').date()},'tb_sse_info')
        print u'大盘价格完毕'
    else:
        print u'大盘价格抓取失败'

# travelSSE()

# print time.mktime(time.strptime('20151001','%Y%m%d'))




